/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JoViewPanel.java
 *
 * Created on 29/11/2011, 22:33:14
 */

package proj5labbd.graphics.jornal;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import proj5labbd.DBInterface;
import proj5labbd.graphics.abstractOpGraphics.OperationPanel;

public class JoViewPanel extends OperationPanel {

    private ArrayList<String> fields;
    /** Creates new form JoViewPanel */
    public JoViewPanel(JornalFrame frame, DBInterface dbInterface) {
        super(frame,dbInterface);
        initComponents();
        fields = new ArrayList<String>();
        JornalAction act = new JornalAction();
        jbtView.addActionListener(act);
        jchkDept.addItemListener(act);
        jchkTitulo.addItemListener(act);
        jchkDesc.addItemListener(act);
        jchkAbr.addItemListener(act);
        jchkDur.addItemListener(act);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpnInsert = new javax.swing.JPanel();
        jlbTitulo = new javax.swing.JLabel();
        jtxtTitulo = new javax.swing.JTextField();
        jtxtDuracao = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jlbAbr = new javax.swing.JLabel();
        jcmbAbr = new javax.swing.JComboBox();
        jlbDept = new javax.swing.JLabel();
        jtxtDept = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbView = new javax.swing.JTable();
        jpbField = new javax.swing.JPanel();
        jchkTitulo = new javax.swing.JCheckBox();
        jchkDur = new javax.swing.JCheckBox();
        jchkDept = new javax.swing.JCheckBox();
        jchkAbr = new javax.swing.JCheckBox();
        jchkDesc = new javax.swing.JCheckBox();
        jbtView = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Visualização"));

        jpnInsert.setBorder(javax.swing.BorderFactory.createTitledBorder("Parâmetros de busca"));

        jlbTitulo.setText("Título");

        jLabel1.setText("Duração");

        jlbAbr.setText("Abrangência");

        jcmbAbr.setModel(new javax.swing.DefaultComboBoxModel(new String[] { " ", "REGIONAL", "ESTADUAL", "NACIONAL", "INTERNACIONAL" }));

        jlbDept.setText("Departamento");

        javax.swing.GroupLayout jpnInsertLayout = new javax.swing.GroupLayout(jpnInsert);
        jpnInsert.setLayout(jpnInsertLayout);
        jpnInsertLayout.setHorizontalGroup(
            jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnInsertLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpnInsertLayout.createSequentialGroup()
                        .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jtxtTitulo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                            .addComponent(jlbTitulo, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtxtDuracao, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
                            .addComponent(jlbAbr)))
                    .addGroup(jpnInsertLayout.createSequentialGroup()
                        .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlbDept)
                            .addComponent(jtxtDept, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jcmbAbr, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jpnInsertLayout.setVerticalGroup(
            jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnInsertLayout.createSequentialGroup()
                .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbTitulo)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtxtDuracao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbDept)
                    .addComponent(jlbAbr))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtDept, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcmbAbr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(43, Short.MAX_VALUE))
        );

        jtbView.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtbView);

        jpbField.setBorder(javax.swing.BorderFactory.createTitledBorder("Campos a selecionar"));

        jchkTitulo.setText("Título");

        jchkDur.setText("Duração");

        jchkDept.setText("Departamento");

        jchkAbr.setText("Abrangência");

        jchkDesc.setText("Descrição");
        jchkDesc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jchkDescActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpbFieldLayout = new javax.swing.GroupLayout(jpbField);
        jpbField.setLayout(jpbFieldLayout);
        jpbFieldLayout.setHorizontalGroup(
            jpbFieldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpbFieldLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpbFieldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jchkTitulo)
                    .addComponent(jchkDur)
                    .addComponent(jchkDept)
                    .addComponent(jchkAbr)
                    .addComponent(jchkDesc))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpbFieldLayout.setVerticalGroup(
            jpbFieldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpbFieldLayout.createSequentialGroup()
                .addComponent(jchkTitulo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jchkDur)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jchkDept)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jchkAbr)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jchkDesc)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        jbtView.setText("Visualizar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 668, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jpnInsert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jpbField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(15, Short.MAX_VALUE))
                    .addComponent(jbtView, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jpnInsert, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jpbField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jbtView))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jchkDescActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jchkDescActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jchkDescActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtView;
    private javax.swing.JCheckBox jchkAbr;
    private javax.swing.JCheckBox jchkDept;
    private javax.swing.JCheckBox jchkDesc;
    private javax.swing.JCheckBox jchkDur;
    private javax.swing.JCheckBox jchkTitulo;
    private javax.swing.JComboBox jcmbAbr;
    private javax.swing.JLabel jlbAbr;
    private javax.swing.JLabel jlbDept;
    private javax.swing.JLabel jlbTitulo;
    private javax.swing.JPanel jpbField;
    private javax.swing.JPanel jpnInsert;
    private javax.swing.JTable jtbView;
    private javax.swing.JTextField jtxtDept;
    private javax.swing.JTextField jtxtDuracao;
    private javax.swing.JTextField jtxtTitulo;
    // End of variables declaration//GEN-END:variables

    class JornalAction implements ActionListener, ItemListener {

         /*
         * Controla os eventos de visualização da tabela jornal
         */
         public void actionPerformed(ActionEvent e) {
            
            String [] titles = {"ID","Titulo","Descrição","ID do departamento","Duração","Abrangência"};
            ArrayList<String> tables = new ArrayList<String>();
            tables.add("jornal");
            boolean join = false;
            
            try {
                ArrayList<String> param = new ArrayList<String>();
                String s[][];
                
                if (jtxtTitulo.getText().trim().compareTo("") != 0) {
                    param.add("UPPER(jornal.tituloPr)=UPPER('"+jtxtTitulo.getText()+"')");                
                }
                if (jtxtDept.getText().trim().compareTo("") != 0) {
                    param.add("departamento.idDe=jornal.idDe");
                    param.add("UPPER(departamento.nomeDe)=UPPER('"+jtxtDept.getText()+"')");
                    join = true;
                    tables.add("departamento");
                }
                if (jtxtDuracao.getText().trim().compareTo("") != 0) {
                    param.add("UPPER(jornal.duracaoJo)=UPPER('"+jtxtDuracao.getText()+"')");
                }
                if (jcmbAbr.getSelectedItem().toString().compareTo(" ") != 0) {
                    param.add("UPPER(jornal.abrangenciaJo)=UPPER('"+jcmbAbr.getSelectedItem().toString()+"')");
                }

                if (param.size() > 0 ) {
                        
                        if (fields.size() > 0) {
                            
                            if (jchkDept.isSelected() && !join) {
                                    param.add("departamento.idDe=jornal.idDe");
                                    tables.add("departamento");
                            }
                       
                            String[] fieldArray = new String[fields.size()];
                            String[] tableArray = new String[tables.size()];
                            String[] paramArray = new String[param.size()];
                            param.toArray(paramArray);
                            tables.toArray(tableArray);
                            fields.toArray(fieldArray);
                            s = dbInterface.view(fieldArray, paramArray, tableArray);
                            jtbView.setModel(
                                    new DefaultTableModel(s,fieldArray)
                                    );
                        }
                        else {
                            String[] paramArray = new String[param.size()];
                            String[] tableArray = new String[tables.size()];
                            tables.toArray(tableArray);
                            param.toArray(paramArray);
                            s = dbInterface.view(null, paramArray, tableArray);
                            jtbView.setModel(
                                    new DefaultTableModel(s,titles)
                                    );
                        }
                    
                 }
                
                else {
                    if (fields.size() > 0) {
                           
                        
                    if (jchkDept.isSelected() && !join) {
                                param.add("departamento.idDe=jornal.idDe");
                                tables.add("departamento");
                        }

                        String[] fieldArray = new String[fields.size()];
                        String[] tableArray = new String[tables.size()];
                        String[] paramArray = new String[param.size()];    
                        tables.toArray(tableArray);
                        fields.toArray(fieldArray);
                        param.toArray(paramArray);
                        s = dbInterface.view(fieldArray, paramArray, tableArray);
                        jtbView.setModel(
                                new DefaultTableModel(s,fieldArray)
                                );
                    }
                    else {
                    String[] tableArray = new String[tables.size()];
                    tables.toArray(tableArray);
                    s = dbInterface.view(null, null, tableArray);
                    jtbView.setModel(
                                    new DefaultTableModel(s,titles)
                                    );
                    }
                }
            }
            catch (SQLException ex) {
                frame.printException(ex);
                JOptionPane.showMessageDialog(null, "Erro ao resgatar as tuplas desejadas.");
            }
            finally {
                join = false;
            }
            
        }
         
         /*
          * Controla os eventos relacionados às check box
          */
         public void itemStateChanged(ItemEvent ie) {
            if ( ie.getStateChange() == ItemEvent.SELECTED ) {
                if (ie.getSource() == jchkDept) {
                    fields.add("departamento.nomeDe");
                }
                if (ie.getSource() == jchkDesc) {
                    fields.add("jornal.descricaoPr");
                }
                if (ie.getSource() == jchkTitulo) {
                    fields.add("jornal.tituloPr");
                }
                if (ie.getSource() == jchkAbr) {
                    fields.add("jornal.abrangenciaJo");
                }
                if (ie.getSource() == jchkDur) {
                    fields.add("jornal.duracaoJo");
                }
               
            }
            if ( ie.getStateChange() == ItemEvent.DESELECTED ) {
                if (ie.getSource() == jchkDept) {
                    fields.remove("departamento.nomeDe");
                }
                if (ie.getSource() == jchkDesc) {
                    fields.remove("jornal.descricaoPr");
                }
                if (ie.getSource() == jchkTitulo) {
                    fields.remove("jornal.tituloPr");
                }
                if (ie.getSource() == jchkAbr) {
                    fields.remove("jornal.abrangenciaJo");
                }
                if (ie.getSource() == jchkDur) {
                    fields.remove("jornal.duracaoJo");
                }
            }
        }

    }

}
